package org.bizming.base.service;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.bizming.base.BaseModel;

public class MysqlHelper implements SqlHelper {

	public String sqlAdd(BaseModel o, List<Object> values, String tableName) {
		Map<String, Field> map = buildMap(o);

		StringBuilder sbColum = new StringBuilder(25);
		StringBuilder sbValue = new StringBuilder(100);

		Field field;
		Object value = null;
		for (Map.Entry<String, Field> entry : map.entrySet()) {
			field = entry.getValue();
			try {
				value = field.get(o);
			} catch (Exception e) {
				e.printStackTrace();
				value = null;
			}
			sbColum.append(entry.getKey()).append(",");
			sbValue.append("?").append(",");
			values.add(value);

		}
		StringBuilder builder = new StringBuilder(100);
		builder.append("insert into ").append(tableName);
		builder.append("(").append(sbColum.substring(0, sbColum.length() - 1));
		builder.append(") values (");
		builder.append(sbValue.substring(0, sbValue.length() - 1)).append(")");

		return builder.toString();
	}

	public String sqlUpdate(BaseModel o, List<Object> values, String tableName) {
		Map<String, Field> map = buildMap(o);

		StringBuilder sbColum = new StringBuilder(25);
		StringBuilder sbValue = new StringBuilder(100);

		Field field;
		Object value = null;
		for (Map.Entry<String, Field> entry : map.entrySet()) {
			field = entry.getValue();
			try {
				value = field.get(o);
			} catch (Exception e) {
				e.printStackTrace();
				value = null;
			}
			if (value != null && !(value instanceof BaseModel)) {
				sbColum.append(entry.getKey()).append(",");
				sbValue.append("?").append(",");
				values.add(value);
			}
			sbColum.append(entry.getKey()).append("=?,");
			sbValue.append("?").append(",");
			values.add(value);
		}
		StringBuilder builder = new StringBuilder(100);
		builder.append("update ").append(tableName).append(" set ");
		builder.append(sbColum.substring(0, sbColum.length() - 1));
		builder.append(" where ").append("id=?");

		return builder.toString();
	}

	public String sqlRemove(String tableName) {
		StringBuilder builder = new StringBuilder(100);
		builder.append("delete from ").append(tableName).append(" where id=? ");
		return builder.toString();
	}

	public String sqlGet(BaseModel o, String tableName) {
		StringBuilder builder = new StringBuilder(100);
		builder.append("select * from ").append(tableName).append(" where id=? ");
		return builder.toString();
	}

	public String sqlCount(String tableName) {
		StringBuilder builder = new StringBuilder(100);
		builder.append("select count(id) from ").append(tableName);
		return builder.toString();
	}

	public <T> String sqlSelectList(Class<T> cls, String tableName) {
		Map<String, Field> map = buildMap(cls);

		StringBuilder column = new StringBuilder(100);
		for (Map.Entry<String, Field> entry : map.entrySet()) {
			column.append(entry.getKey()).append(",");
		}

		StringBuilder builder = new StringBuilder(100);
		builder.append("select ").append("id,").append(column.substring(0, column.length() - 1))
				.append(" from ").append(tableName);
		builder.append(" limit ?,?");

		return builder.toString();
	}

	@SuppressWarnings("unused")
	public void clear(Object objects[]) {
		if (objects != null) {
			for (Object o : objects) {
				o = null;
			}
		}
		objects = null;
	}

	@SuppressWarnings("unused")
	public void clear(List<Object> list) {
		if (list != null) {
			for (Object o : list) {
				o = null;
			}
		}
		list = null;
	}

	/** 将对象转成map */
	public Map<String, Field> buildMap(BaseModel o) {
		Map<String, Field> map = new HashMap<String, Field>();
		Field[] fields = o.getClass().getDeclaredFields();
		for (Field field : fields) {
			field.setAccessible(true);
			map.put(field.getName(), field);
		}
		return map;
	}

	/** 将对象转成map */
	private <T> Map<String, Field> buildMap(Class<T> cls) {
		Map<String, Field> map = new HashMap<String, Field>();
		Field[] fields = cls.getDeclaredFields();
		for (Field field : fields) {
			field.setAccessible(true);
			map.put(field.getName(), field);
		}
		return map;
	}
}
